package com.dep.controller;

import com.dep.aspect.Log;
import com.dep.aspect.Token;
import com.dep.common.BeanValidator;
import com.dep.common.CodeIdEnum;
import com.dep.common.PageRequest;
import com.dep.common.ResponseData;
import com.dep.dto.req.DepartmentInsertReq;
import com.dep.dto.req.DepartmentReq;
import com.dep.dto.req.StaffInsertReq;
import com.dep.dto.resp.DepartmentAllResp;
import com.dep.dto.resp.DepartmentResp;
import com.dep.entity.Department;
import com.dep.entity.Staff;
import com.dep.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;

@RestController
@RequestMapping("/department/")
@Api(tags = "部门接口(token和sign暂时不设置)")
@Slf4j
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @Log
    @ApiOperationSupport(author = "黄伶通")
    @GetMapping("/all")
    @ApiOperation(value = "查询所有部门")
    @Token
    public ResponseData<List<DepartmentAllResp>> versionPage() {
        return departmentService.selectAllDep();
    }

    @Log
    @ApiOperationSupport(author = "黄伶通")
    @GetMapping("/page")
    @ApiOperation(value = "分页查询部门信息")
    @Token
    public ResponseData<PageInfo<DepartmentResp>> findDeps(DepartmentReq req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return departmentService.findDepartmentPage(req);
    }

    @Log
    @ApiOperationSupport(author = "黄伶通")
    @PostMapping("/insert")
    @ApiOperation(value = "录入部门")
    @Token
    public ResponseData<Staff> insertStaff(@RequestBody DepartmentInsertReq req) throws Exception{
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        return departmentService.insert(req);
    }

}
